package com.hp.demo.vteam.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hp.demo.vteam.bean.InforKind;
import com.hp.demo.vteam.bean.Information;

public class InforManagerServlet extends BaseServlet {
	private static final long serialVersionUID = -393778548946444384L;

	@Override
	public void edit(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
        String name = request.getParameter("name");
        String txt=request.getParameter("txt");
        String desc = request.getParameter("desc");
        String inforKindId = request.getParameter("inforKindId");
        
    	Key inforKindKey = KeyFactory.createKey(InforKind.class.getSimpleName(),Long.parseLong(inforKindId));
        Date date = new Date();
        InforKind inforKind = pm.getObjectById(InforKind.class,inforKindKey);
        Information information = null;
        if(id!=null && !id.isEmpty()){
        	information = pm.getObjectById(Information.class, Long.parseLong(id));
        	information.setName(name);
        	information.setTxt(txt);
        	information.setDesc(desc);
        	information.setAddedTime(date);
        	information.setAddedBy(user);
        	information.setInforKind(inforKind);
        	inforKind.getInformations().add(information);
        }
       	if(information == null){
       		information = new Information(name,txt,desc, user, date,inforKind);
       		inforKind.getInformations().add(information);
        }
        super.baseEdit(request, response, inforKind);
		response.getWriter().print(String.format(SUCCESS_MSG, "true","操作成功" ));
	}

	@Override
	public void del(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
        Information inforManager = null;

        if(id!=null && !id.isEmpty()){
        	inforManager = pm.getObjectById(Information.class, Long.parseLong(id));
        }
        System.out.println(inforManager);
        super.baseDel(request, response, inforManager);
		response.getWriter().print(String.format(SUCCESS_MSG, "true","操作成功" ));
		response.getWriter().print("{success: true}");
	}

	@Override
	public void search(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		super.baseSearch(request, response, Information.class);

	}

}
